//
//  YDYFanJuView.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/9/28.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYFanJuView.h"
#import "YDYFanJuHeaderReusableView.h"
#import "YDYFanju.h"
#import "YDYFanJuNewsView.h"
#import "YDYFanJuFlowLayout.h"
#import <AFNetworking.h>


@interface YDYFanJuView()<UICollectionViewDelegate,UICollectionViewDataSource,YDYFanJuFlowLayoutDelegate>
/** 显示内容 */
@property (nonatomic, weak) UICollectionView *contentCollectionView;
/** 顶部视图 */
@property (nonatomic, weak) YDYFanJuHeaderReusableView *headerview;
/** 数据源，存在所有的番剧 */
@property (nonatomic, strong) NSArray *dataArray;
@end


static NSString * const ydy_FanjuHeaderReuseIdentifier = @"FanJuHeader";
static NSString * const ydy_FanjuCollectionViewCellIdetifier = @"ydy_FanjuCollectionViewCellIdetifier";


@implementation YDYFanJuView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        
        [self setupCollectionview];
        //注册，监听都在这个方法
        [self setupDefault];
        [self getDatafromUrl];
  
    }
    return self;
}
-(void)setupDefault{
   
  
    [self.contentCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ydy_FanjuCollectionViewCellIdetifier];
    [self.contentCollectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YDYFanJuHeaderReusableView class]) bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ydy_FanjuHeaderReuseIdentifier];
}
-(void)setupCollectionview{
    YDYFanJuFlowLayout *fanjulayout = [[YDYFanJuFlowLayout alloc]init];
    fanjulayout.delegate = self;
    
    UICollectionView *collectionview = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)collectionViewLayout:fanjulayout];

    collectionview.backgroundColor = [UIColor colorWithRed:235.0 /255.0 green:235.0/255.0 blue:235.0/255.0 alpha:1];
    collectionview.delegate = self;
    collectionview.dataSource = self;

    self.contentCollectionView = collectionview;
    [self addSubview:collectionview];
}


#pragma mark -
#pragma mark -
-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.contentCollectionView reloadData];
    
}

#pragma mark -
#pragma mark -collectionview datasource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{;
    return self.dataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell *cell = [self.contentCollectionView dequeueReusableCellWithReuseIdentifier:ydy_FanjuCollectionViewCellIdetifier forIndexPath:indexPath];
    for (UIView *view in cell.subviews) {
        [view removeFromSuperview];
    }
    
    
    
    YDYFanJuNewsView *fanjuview = [[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([YDYFanJuNewsView class]) owner:nil options:nil]lastObject];
    fanjuview.frame = cell.bounds;
    fanjuview.fanju = self.dataArray[indexPath.row];
    [cell addSubview:fanjuview];
    cell.layer.cornerRadius = 10;
    cell.clipsToBounds = YES;
    
    return cell;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    YDYFanJuHeaderReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ydy_FanjuHeaderReuseIdentifier forIndexPath:indexPath];
//    header.backgroundColor = [UIColor redColor];
    self.headerview = header;
    return header;
    
}

#pragma mark -
#pragma mark -fanjulayout delegate
-(CGFloat)showFlowlayoutwithHeight:(YDYFanJuFlowLayout *)layout layoutWidth:(CGFloat)width andIndexpath:(NSIndexPath *)indexpath{
    YDYFanju *fanju = self.dataArray[indexpath.row];
    
    return width * 0.8 + [fanju.title boundingRectWithSize:CGSizeMake(width - 16, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:13.0] } context:nil].size.height;;
}


#pragma mark -
#pragma mark -网络请求
-(void)getDatafromUrl{
    
    //    @"http://api.bilibili.com/online_list?_device=android&_hwid=130a7709aeac1793&appkey=c1b107428d337928&platform=android&typeid=13&sign=cb5cf6d54ed92fc25c4a8b4292a46692"
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *url = @"http://api.bilibili.com/online_list";
    NSDictionary *parameters = @{@"_device=":@"android",
                                 @"_hwid=":@"130a7709aeac1793",
                                 @"appkey=":@"c1b107428d337928",
                                 @"platform=":@"android",
                                 @"typeid=":@"13",
                                 @"sign=":@"cb5cf6d54ed92fc25c4a8b4292a46692"};
    
    [manager GET:url parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        NSArray *listarray = responseObject[@"list"];
        NSMutableArray *temp = [NSMutableArray arrayWithCapacity:30];
        for (NSDictionary *dict in listarray) {
            YDYFanju *fanju = [YDYFanju ydyFanjuWithDict:dict];
            [temp addObject:fanju];
        }
        self.dataArray = temp;
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        YDYLog(@"番剧网络请求错误=%@",error);
    }];
}
@end
